package com.china08.yxyapi.repository.mg.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.util.Assert;

import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;
import com.china08.yxyapi.util.MongodbUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

public class YxyNoticeInnerRepositoryImpl {

    @Autowired
    private MongoTemplate mongoTemplate;

    Page<YxyNoticeInner> findBySchoolIdAndReceiverUserIdAndReceiverIsDeleteAndReceiverIsRead1(String schoolId,
            String userId, int isDelete, int receiverIsRead, Pageable pageable) {
        DBCollection dbCollection = mongoTemplate.getCollection(mongoTemplate.getCollectionName(YxyNoticeInner.class));
        BasicDBObject dbObject = new BasicDBObject("school_id", schoolId);
        List<DBObject> list = new ArrayList<DBObject>();
        list.add(new BasicDBObject("userId", userId).append("isRead", receiverIsRead).append("isDelete", isDelete));
        dbObject.put("receiver", list);
        Page<YxyNoticeInner> pyx = MongodbUtil.getPage(dbCollection, dbObject, pageable, YxyNoticeInner.class);

        return pyx;

    }

    Page<YxyNoticeInner> findBySchoolIdAndReceiverUserIdAndReceiverIsDeleteAndReceiverIsRead(String schoolId,
            String userId, int isDelete, int receiverIsRead, Pageable pageable) {
        DBCollection dbCollection = mongoTemplate.getCollection(mongoTemplate.getCollectionName(YxyNoticeInner.class));
        Assert.notNull(schoolId, "学校id不能为空！ ");
        Assert.notNull(userId, "用户id不能空！ ");
        String msql = "{'school_id':'%s','receiver':{'$elemMatch': {'userId':'%s','isRead':%s,'isDelete':%s}}}";

        return MongodbUtil.getPage(dbCollection,
                BasicDBObject.parse(String.format(msql, schoolId, userId, receiverIsRead, isDelete)), pageable,
                YxyNoticeInner.class);
    }

}
